/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.desktop.paneles;

import com.google.gson.Gson;
import javax.swing.JOptionPane;

/**
 *
 * @author Yanira
 */
public class EliminarExpositor extends javax.swing.JPanel {

    /**
     * Creates new form EliminarExpositor
     */
    private AdmEvent padre;
    private String noEltos = "No hay expositores en el evento";
    public EliminarExpositor(AdmEvent father) {
        padre = father;
        initComponents();
        this.expositores();
    }
    
    public void expositores()
    {
        Gson gson = new Gson();
        String [] expositores = gson.fromJson(listaExpositores(), String[].class);
        if(expositores==null)//se toma que no contiene expositores
        {
            this.jComboBox1.addItem(noEltos);
        }
        else
        {
            if(expositores.length>0)
            {
                for(String a:expositores)
                {
                    this.jComboBox1.addItem(a);
                }
            }
            else
            {
                this.jComboBox1.addItem(noEltos);
            }
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(602, 548));

        jLabel1.setText("Seleccione Expositor que desea eliminar:");

        jButton2.setText("Cancelar");
        jButton2.setMaximumSize(new java.awt.Dimension(75, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(75, 25));
        jButton2.setPreferredSize(new java.awt.Dimension(75, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Aceptar");
        jButton1.setMaximumSize(new java.awt.Dimension(75, 25));
        jButton1.setMinimumSize(new java.awt.Dimension(75, 25));
        jButton1.setPreferredSize(new java.awt.Dimension(75, 25));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 388, Short.MAX_VALUE))
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 449, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        //boton Cancelar
        padre.jSplitPane4.setRightComponent(new BienvExpositor(Temporal.evento));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String expositor = (String)jComboBox1.getSelectedItem();
        if(expositor.equals(noEltos))
        {
            JOptionPane.showMessageDialog(this, "Operacion invalida, no hay expositores", "Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            int i = JOptionPane.showConfirmDialog(this, "¿Realmente desea eliminar el Expositor "+expositor+" ?","Eliminar Expositor", JOptionPane.YES_NO_OPTION);
            if(i==0)
            {
                if(this.validarExpositoEliminado(expositor)!=null)
                {
                    JOptionPane.showMessageDialog(this, "No se pudo eliminar"+expositor+"ya que esta asignado en"+validarExpositoEliminado(expositor), "Error",JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    this.eliminarExpositor(expositor);
                    JOptionPane.showMessageDialog(this,"Expositor Eliminado Exitosamente");
                    jComboBox1.removeAllItems();
                    expositores();
                }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    public String validarExpositoEliminado(String expositor)
    {
        Gson gson = new Gson();
        String[] actividades;
        String[] listaExpo;
        String[] eventos = gson.fromJson(listaEventos(), String[].class);
        if(eventos!=null)
        {
            for(String e:eventos)//se indexa a los eventos
            {
                actividades = gson.fromJson(listarActividades(e), String[].class);
                if(actividades!=null)
                {
                    for(String a:actividades)//se indexa a las actividades
                    {
                        listaExpo = gson.fromJson(listaExpositoresEvento(e,a), String[].class);
                        for(String n : listaExpo)
                        if(n.equals(expositor))//datosActividad[3] contiene el expositor la actividad que se analiza
                        {
                            return e;
                        }
                    }   
                }
            }
            return null;
        }
        return null;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    
  

    private static String listaExpositoresEvento(java.lang.String evento, java.lang.String actividad) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaExpositoresEvento(evento, actividad);
    }

    private static String datosActividad(java.lang.String evento, java.lang.String actividad) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.datosActividad(evento, actividad);
    }

    private static void eliminarActividad(java.lang.String evento, java.lang.String actividad) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        port.eliminarActividad(evento, actividad);
    }

    private static String listarActividades(java.lang.String evento) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listarActividades(evento);
    }

    private static String listaEventos() {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaEventos();
    }

    private static String listaExpositores() {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaExpositores();
    }

    private static boolean eliminarExpositor(java.lang.String nombre) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.eliminarExpositor(nombre);
    }

  
    
}
